//
//  WBWelcomeView.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class WBWelcomeView: WBBaseView {

    //创建UIImageView
    lazy var iconImageView: UIImageView = {
        let iconImageView = UIImageView()
        iconImageView.wb_setImageWith(urlStr: WBUserAccount.shared.avatar_large!, placeHolder: "avatar_default_big")
        return iconImageView
    }()
    
    //创建Label
    lazy var welcomeLabel: UILabel = UILabel(title: "\(WBUserAccount.shared.screen_name!), 欢迎归来", fontSize: 17, alignMent: .center)
    
    override func wbSetupSubviews() {
       super.wbSetupSubviews()
       setupUI()
    }
    
    override func didMoveToWindow() {
//        print(#function)
        //添加弹力动画
        addAnimation()
    }

}

extension WBWelcomeView {
    fileprivate func setupUI () {
        self.frame = screenBounds
        self.backgroundColor = UIColor.white
        
        //添加子控件
        addSubview(iconImageView)
        addSubview(welcomeLabel)
        
        //设置头像的圆角
        iconImageView.layer.cornerRadius = 40
        iconImageView.layer.masksToBounds = true
        
        //约束
        iconImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-100)
            make.size.equalTo(CGSize(width: 80, height: 80))
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(100)
        }
        
        //更新约束
        layoutIfNeeded()
    }
    
    fileprivate func addAnimation() {
        //如果需要二次修改一个控件的约束, 需要使用update
        //用update只需要修改必要的值
        //用update只能修make中设置过的值
        iconImageView.snp.updateConstraints { (make) in
            make.centerY.equalTo(self).offset(-150)
        }
        
        //initialSpringVelocity: 重力系数
        //usingSpringWithDamping: 弹力系数: 从0到1, 越小越弹
        UIView.animate(withDuration: 3.0, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 9.8, options: [], animations: {
            
            //调用该方法会强制调用layoutsubViews, 在layoutsubViews里会让布局生效
            self.layoutIfNeeded()
            
        }, completion: {
            (finish) in
            UIView.animate(withDuration: 1.1, animations: {
                //透明
                self.alpha = 0;
            }, completion: { (finish) in
                self.removeFromSuperview()
            })
        })
    }
}
